package com.jungle.design.mode.strategypattern.strategy.factory;

import com.jungle.design.mode.strategypattern.entity.enums.PayEnum;
import com.jungle.design.mode.strategypattern.strategy.PayStrategy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jungle
 * @create 2024-09-15 22:25
 * 策略工厂注入所有的策略对象
 */

@Component
public class PayStrategyFactory {
    public final ConcurrentHashMap<PayEnum, PayStrategy> handlersStrategyMap = new ConcurrentHashMap<>();

    /**
     * 初始化map
     */
    public PayStrategyFactory(List<PayStrategy> payStrategyList) {
        payStrategyList.forEach(item -> handlersStrategyMap.put(item.getHandleStrategy(), item));

    }

    /**
     * 获取策略对象的方法
     */
    public PayStrategy getPayStrategy(PayEnum payEnum) {
        return handlersStrategyMap.get(payEnum);
    }
}
